<template>
  <div class="cu-common-layout crmcustomer">
    <div class="cu-common-layout-center">
      <el-row class="cu-common-search-box" :gutter="10">
        <el-form @submit.native.prevent>
          <el-col :span="5">
            <el-form-item label="所在区域" prop="area">
              <custom-address v-model="query.area" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="客户信息" prop="info">
              <el-input v-model="query.info" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="咨询日期" prop="consultTime">
              <el-date-picker v-model="query.consultTime" type="daterange" value-format="timestamp" format="yyyy-MM-dd" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="咨询渠道" prop="channel">
              <el-select v-model="query.channel" clearable required :style="{ width: '100%' }" filterable>
                <el-option-group v-for="group in channelOptions" :key="group.id" :label="group.fullName">
                  <el-option v-for="item in group.children" :key="item.id" :label="item.fullName" :value="item.enCode"></el-option>
                </el-option-group>
              </el-select>
            </el-form-item>
          </el-col>
          <template v-if="showAll">
            <el-col :span="5">
              <el-form-item label="客户类别" prop="category">
                <custom-tree-select
                  v-model="query.category"
                  :options="categoryOptions"
                  placeholder="请选择"
                  lastLevel
                  clearable
                  required
                  :props="{
                    value: 'enCode', // ID字段名
                    label: 'fullName', // 显示名称
                    children: 'children', // 子级字段名
                    disabled: 'selectable'
                  }"
                />
              </el-form-item>
            </el-col>
          </template>
          <el-col :span="4">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()" native-type="submit">{{ $t('common.search') }}</el-button>
              <el-button icon="el-icon-refresh-right" @click="reset()">
                {{ $t('common.reset') }}
              </el-button>
              <el-button type="text" icon="el-icon-arrow-down" @click="showAll = true" v-if="!showAll">展开</el-button>
              <el-button type="text" icon="el-icon-arrow-up" @click="showAll = false" v-else>收起</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="cu-common-layout-main cu-flex-main">
        <el-tabs v-model="query.status" type="border-card" class="customerTabs" @tab-click="handleTabClick()">
          <div class="cu-common-head">
            <div class="left-btn">
              <top-opts @add="addOrUpdateHandle()" :disabled="isdisabled">
                <el-button v-has="'btn_transfer'" size="mini" type="primary" icon="el-icon-refresh-left" :disabled="isdisabled" @click="transferHandle()">
                  重新分配
                </el-button>
                <el-button v-if="query.status != 5 && query.status != 2" v-has="'btn_back'" size="mini" type="danger" icon="el-icon-back" :disabled="isdisabled" @click="backHandle()">
                  批量退回
                </el-button>
                <el-button v-if="query.status == 1" v-has="'btn_up'" size="mini" type="success" icon="el-icon-s-flag" :disabled="isdisabled" @click="upgradeHandle()">
                  重点跟进
                </el-button>
                <el-button v-if="query.status != 1" v-has="'btn_follow'" size="mini" type="success" icon="el-icon-d-arrow-left" :disabled="isdisabled" @click="followHandle()">
                  退回跟进
                </el-button>
              </top-opts>
            </div>
            <div class="cu-common-head-right">
              <el-tooltip effect="dark" content="刷新" placement="top">
                <el-link icon="icon-ym icon-ym-Refresh cu-common-head-icon" :underline="false" @click="reset()" />
              </el-tooltip>
              <screenfull isContainer />
            </div>
          </div>
          <el-tab-pane label="跟进客户" name="1" id="tab_customer" v-if="custom.hasBtnP('tab_customer')">
            <custom-table v-if="query.status == '1'" v-loading="listLoading" :data="list" :hasC="true" @selection-change="handleSelectionChange">
              <el-table-column prop="name" label="咨询人" align="left" v-if="custom.hasP('name')" width="100" show-overflow-tooltip></el-table-column>
              <el-table-column label="联系方式" width="120" v-if="custom.hasP('mobile')" align="left">
                <template slot-scope="scope">
                  <span>
                    <i v-if="scope.row.mobile != null" class="ym-custom ym-custom-cellphone"></i>
                    {{ scope.row.mobile }}
                  </span>
                  <span>
                    <i v-if="scope.row.wechat != null" class="ym-custom ym-custom-wechat"></i>
                    {{ scope.row.wechat }}
                  </span>
                </template>
              </el-table-column>
              <el-table-column prop="province" label="省份" align="left" v-if="custom.hasP('province')" width="100" show-overflow-tooltip />
              <el-table-column prop="city" label="城市" align="left" v-if="custom.hasP('province')" width="100" show-overflow-tooltip />
              <el-table-column prop="receiveTime" label="分配时间" align="center" v-if="custom.hasP('receiveTime')" :formatter="custom.tableDateTimeFormat" width="120" />
              <el-table-column prop="lastConsultTime" label="咨询时间" align="center" v-if="custom.hasP('lastConsultTime')" :formatter="custom.tableDateTimeFormat" width="120" />
              <el-table-column prop="channel" label="咨询渠道" align="left" v-if="custom.hasP('channel')" width="120" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.channel | dynamicTreeCodeText(channelOptions) }}
                </template>
              </el-table-column>
              <el-table-column prop="category" label="客户类别" align="left" v-if="custom.hasP('category')" width="120" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.category | dynamicTreeCodeText(categoryOptions) }}
                </template>
              </el-table-column>
              <el-table-column prop="lastTrack" label="最近跟进" align="left" width="300" v-if="custom.hasP('lastTrack')" show-overflow-tooltip />
              <el-table-column prop="focusAttention" label="关注的重点" align="left" v-if="custom.hasP('category')" width="120" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.focusAttention | dynamicCodeText(focusAttentionOptions) }}
                </template>
              </el-table-column>
              <el-table-column prop="followupSummary" label="跟进情况概述" align="left" min-width="200" v-if="custom.hasP('followupSummary')" show-overflow-tooltip />
              <el-table-column prop="createTime" label="录入时间" align="center" v-if="custom.hasP('createTime')" :formatter="custom.tableDateTimeFormat" width="120" />
              <el-table-column prop="createUserName" label="录入人员" align="left" v-if="custom.hasP('createUserName')" width="80" show-overflow-tooltip />
              <el-table-column prop="updateTime" label="修改时间" align="left" v-if="custom.hasP('updateTime')" :formatter="custom.tableDateTimeFormat" width="120" />
              <el-table-column prop="updateUserName" label="修改人员" align="left" v-if="custom.hasP('updateUserName')" width="80" />
              <el-table-column label="操作" fixed="right" width="220" align="center">
                <template slot-scope="scope">
                  <table-opts :editDisabled="isdisabled" :delDisabled="isdisabled" @edit="addOrUpdateHandle(scope.row.id)" @del="handleDel(scope.row.id)">
                    <el-button type="text" size="mini" @click="addSubFormHandle(scope.row.id)">跟进</el-button>
                    <!-- <el-button type="text" size="mini" :disabled="isdisabled" @click.native="addHandle(scope.row)">
                      付定金
                    </el-button> -->
                    <el-button type="text" size="mini" @click.native="orderHandle(scope.row)">
                      缴费
                    </el-button>
                    <!-- <el-button type="text" size="mini" @click.native="signHandle(scope.row)">
                      签约
                    </el-button> -->
                  </table-opts>
                </template>
              </el-table-column>
            </custom-table>
          </el-tab-pane>
          <el-tab-pane label="重点客户" name="3" id="tab_important" v-if="custom.hasBtnP('tab_important')">
            <custom-table v-if="query.status == '3'" v-loading="listLoading" :data="list" :hasC="true" @selection-change="handleSelectionChange">
              <el-table-column prop="name" label="咨询人" align="left" v-if="custom.hasP('name')" width="100" show-overflow-tooltip></el-table-column>
              <el-table-column label="联系方式" width="120" v-if="custom.hasP('mobile')" align="left">
                <template slot-scope="scope">
                  <span>
                    <i v-if="scope.row.mobile != null" class="ym-custom ym-custom-cellphone"></i>
                    {{ scope.row.mobile }}
                  </span>
                  <span>
                    <i v-if="scope.row.wechat != null" class="ym-custom ym-custom-wechat"></i>
                    {{ scope.row.wechat }}
                  </span>
                </template>
              </el-table-column>
              <el-table-column prop="category" label="客户类别" align="left" v-if="custom.hasP('category')" width="120" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.category | dynamicTreeCodeText(categoryOptions) }}
                </template>
              </el-table-column>
              <el-table-column prop="lastTrack" label="最近跟进" align="left" width="300" v-if="custom.hasP('lastTrack')" show-overflow-tooltip />
              <el-table-column prop="focusAttention" label="关注的重点" align="left" v-if="custom.hasP('category')" width="120" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.focusAttention | dynamicCodeText(focusAttentionOptions) }}
                </template>
              </el-table-column>
              <el-table-column prop="followupSummary" label="跟进情况概述" align="left" min-width="200" v-if="custom.hasP('lastTrack')" show-overflow-tooltip />
              <el-table-column prop="province" label="省份" align="left" v-if="custom.hasP('province')" width="100" show-overflow-tooltip />
              <el-table-column prop="city" label="城市" align="left" v-if="custom.hasP('province')" width="100" show-overflow-tooltip />
              <el-table-column prop="gender" label="性别" align="left" v-if="custom.hasP('gender')">
                <template slot-scope="scope">
                  {{ scope.row.gender | dynamicCodeText(genderOptions) }}
                </template>
              </el-table-column>
              <el-table-column prop="age" label="年龄" align="left" v-if="custom.hasP('age')" />
              <el-table-column prop="schools" label="意向学校" align="left" v-if="custom.hasP('schools')" />
              <el-table-column prop="createTime" label="录入时间" align="center" v-if="custom.hasP('createTime')" :formatter="custom.tableDateTimeFormat" width="120" />
              <el-table-column prop="createUserName" label="录入人员" align="left" v-if="custom.hasP('createUserName')" width="80" />
              <el-table-column prop="updateTime" label="修改时间" align="left" v-if="custom.hasP('updateTime')" :formatter="custom.tableDateTimeFormat" width="120" />
              <el-table-column prop="updateUserName" label="修改人员" align="left" v-if="custom.hasP('updateUserName')" width="80" />
              <el-table-column label="操作" fixed="right" width="220" align="center">
                <template slot-scope="scope">
                  <table-opts :editDisabled="isdisabled" :delDisabled="isdisabled" @edit="addOrUpdateHandle(scope.row.id)" @del="handleDel(scope.row.id)">
                    <el-button type="text" @click="addSubFormHandle(scope.row.id)" size="mini">跟进</el-button>
                    <!-- <el-button type="text" size="mini" :disabled="isdisabled" @click.native="depositHandle(scope.row)">
                      付定金
                    </el-button> -->
                    <el-button type="text" size="mini" @click.native="orderHandle(scope.row)">
                      缴费
                    </el-button>
                    <!-- <el-button type="text" size="mini" @click.native="signHandle(scope.row)">
                      签约
                    </el-button> -->
                  </table-opts>
                </template>
              </el-table-column>
            </custom-table>
          </el-tab-pane>
          <el-tab-pane label="定金客户" name="4" id="tab_deposit" v-if="custom.hasBtnP('tab_deposit')">
            <custom-table v-if="query.status == '4'" v-loading="listLoading" :data="list" :hasC="true" @selection-change="handleSelectionChange">
              <el-table-column prop="name" label="咨询人" align="left" v-if="custom.hasP('name')" width="100" show-overflow-tooltip></el-table-column>
              <el-table-column label="联系方式" width="120" v-if="custom.hasP('mobile')" align="left">
                <template slot-scope="scope">
                  <span>
                    <i v-if="scope.row.mobile != null" class="ym-custom ym-custom-cellphone"></i>
                    {{ scope.row.mobile }}
                  </span>
                  <span>
                    <i v-if="scope.row.wechat != null" class="ym-custom ym-custom-wechat"></i>
                    {{ scope.row.wechat }}
                  </span>
                </template>
              </el-table-column>
              <!-- <el-table-column prop="mobile" label="联系电话" align="center" v-if="custom.hasP('mobile')" width="100" />
              <el-table-column prop="wechat" label="微信" align="left" v-if="custom.hasP('wechat')" width="100" show-overflow-tooltip /> -->
              <el-table-column prop="lastTrack" label="最近跟进" align="left" width="300" v-if="custom.hasP('lastTrack')" show-overflow-tooltip />
              <el-table-column prop="focusAttention" label="关注的重点" align="left" v-if="custom.hasP('category')" width="120" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.focusAttention | dynamicCodeText(focusAttentionOptions) }}
                </template>
              </el-table-column>
              <el-table-column prop="followupSummary" label="跟进情况概述" align="left" min-width="200" v-if="custom.hasP('lastTrack')" show-overflow-tooltip />
              <el-table-column prop="category" label="客户类别" align="left" v-if="custom.hasP('category')" width="120" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.category | dynamicTreeCodeText(categoryOptions) }}
                </template>
              </el-table-column>
              <el-table-column prop="province" label="省份" align="left" v-if="custom.hasP('province')" width="100" show-overflow-tooltip />
              <el-table-column prop="city" label="城市" align="left" v-if="custom.hasP('province')" width="100" show-overflow-tooltip />
              <el-table-column prop="gender" label="性别" align="left" v-if="custom.hasP('gender')">
                <template slot-scope="scope">
                  {{ scope.row.gender | dynamicCodeText(genderOptions) }}
                </template>
              </el-table-column>
              <el-table-column prop="age" label="年龄" align="left" v-if="custom.hasP('age')" />
              <el-table-column prop="schools" label="意向学校" align="left" v-if="custom.hasP('schools')" />
              <el-table-column prop="createTime" label="录入时间" align="center" v-if="custom.hasP('createTime')" :formatter="custom.tableDateTimeFormat" width="120" />
              <el-table-column prop="createUserName" label="录入人员" align="left" v-if="custom.hasP('createUserName')" width="80" />
              <el-table-column prop="updateTime" label="修改时间" align="left" v-if="custom.hasP('updateTime')" :formatter="custom.tableDateTimeFormat" width="120" />
              <el-table-column prop="updateUserName" label="修改人员" align="left" v-if="custom.hasP('updateUserName')" width="80" />
              <el-table-column label="操作" fixed="right" width="220" align="center">
                <template slot-scope="scope">
                  <table-opts :editDisabled="isdisabled" :delDisabled="isdisabled" @edit="addOrUpdateHandle(scope.row.id)" @del="handleDel(scope.row.id)">
                    <el-button type="text" size="mini" @click="addSubFormHandle(scope.row.id)">跟进</el-button>
                    <el-button type="text" size="mini" @click.native="orderHandle(scope.row)">
                      缴费
                    </el-button>
                    <!-- <el-button type="text" size="mini" @click.native="signHandle(scope.row)">
                      签约
                    </el-button> -->
                  </table-opts>
                </template>
              </el-table-column>
            </custom-table>
          </el-tab-pane>
          <el-tab-pane label="签约客户" name="5" id="tab_clinch" v-if="custom.hasBtnP('tab_clinch')">
            <custom-table v-if="query.status == '5'" v-loading="listLoading" :data="list" :hasC="true" @selection-change="handleSelectionChange">
              <el-table-column prop="name" label="咨询人" align="left" v-if="custom.hasP('name')" width="100" show-overflow-tooltip></el-table-column>
              <el-table-column prop="category" label="客户类别" align="left" v-if="custom.hasP('category')" width="120" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.category | dynamicTreeCodeText(categoryOptions) }}
                </template>
              </el-table-column>
              <el-table-column label="联系方式" width="120" v-if="custom.hasP('mobile')" align="left">
                <template slot-scope="scope">
                  <span>
                    <i v-if="scope.row.mobile != null" class="ym-custom ym-custom-cellphone"></i>
                    {{ scope.row.mobile }}
                  </span>
                  <span>
                    <i v-if="scope.row.wechat != null" class="ym-custom ym-custom-wechat"></i>
                    {{ scope.row.wechat }}
                  </span>
                </template>
              </el-table-column>
              <!-- <el-table-column prop="mobile" label="联系电话" align="center" v-if="custom.hasP('mobile')" width="100" />
              <el-table-column prop="wechat" label="微信" align="left" v-if="custom.hasP('wechat')" width="100" show-overflow-tooltip /> -->
              <el-table-column prop="lastTrack" label="最近跟进" align="left" width="300" v-if="custom.hasP('lastTrack')" show-overflow-tooltip />
              <el-table-column prop="focusAttention" label="关注的重点" align="left" v-if="custom.hasP('category')" width="120" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.focusAttention | dynamicCodeText(focusAttentionOptions) }}
                </template>
              </el-table-column>
              <el-table-column prop="followupSummary" label="跟进情况概述" align="left" min-width="200" v-if="custom.hasP('lastTrack')" show-overflow-tooltip />
              <el-table-column prop="province" label="省份" align="left" v-if="custom.hasP('province')" width="100" show-overflow-tooltip />
              <el-table-column prop="city" label="城市" align="left" v-if="custom.hasP('province')" width="100" show-overflow-tooltip />
              <el-table-column prop="gender" label="性别" align="left" v-if="custom.hasP('gender')">
                <template slot-scope="scope">
                  {{ scope.row.gender | dynamicCodeText(genderOptions) }}
                </template>
              </el-table-column>
              <el-table-column prop="age" label="年龄" align="left" v-if="custom.hasP('age')" />
              <el-table-column prop="retainageDate" label="尾款时间" align="left" v-if="custom.hasP('retainageDate')" :formatter="custom.tableDateTimeFormat" width="120" />
              <el-table-column prop="createTime" label="录入时间" align="center" v-if="custom.hasP('createTime')" :formatter="custom.tableDateTimeFormat" width="120" />
              <el-table-column prop="createUserName" label="录入人员" align="left" v-if="custom.hasP('createUserName')" width="80" />
              <el-table-column prop="updateTime" label="修改时间" align="left" v-if="custom.hasP('updateTime')" :formatter="custom.tableDateTimeFormat" width="120" />
              <el-table-column prop="updateUserName" label="修改人员" align="left" v-if="custom.hasP('updateUserName')" width="80" />
              <el-table-column label="操作" fixed="right" width="150" align="center">
                <template slot-scope="scope">
                  <table-opts :editDisabled="isdisabled" :delDisabled="isdisabled" @edit="addOrUpdateHandle(scope.row.id)" @del="handleDel(scope.row.id)">
                    <el-button type="text" size="mini" @click.native="orderHandle(scope.row)">
                      缴费
                    </el-button>
                    <!-- <el-button type="text" size="mini" @click.native="signHandle(scope.row)">
                      签约
                    </el-button> -->
                    <el-button type="text" size="mini" @click="addSubFormHandle(scope.row.id)">跟进</el-button>
                  </table-opts>
                </template>
              </el-table-column>
            </custom-table>
          </el-tab-pane>
          <el-tab-pane label="退回数据" name="2" id="tab_back" v-if="custom.hasBtnP('tab_back')">
            <custom-table v-if="query.status == 2" v-loading="listLoading" :data="list" :hasC="true" @selection-change="handleSelectionChange">
              <el-table-column prop="name" label="咨询人" align="left" v-if="custom.hasP('name')" width="100" show-overflow-tooltip></el-table-column>
              <el-table-column prop="category" label="客户类别" align="left" v-if="custom.hasP('category')" width="120" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.category | dynamicTreeCodeText(categoryOptions) }}
                </template>
              </el-table-column>
              <el-table-column label="联系方式" width="120" align="left">
                <template slot-scope="scope">
                  <!-- <i :class="icon-ym icon-ym-logo-wechat" /> -->
                  <i v-if="scope.row.mobile != null" class="el-icon-mobile-phone">{{ scope.row.mobile }}</i>
                  <i v-if="scope.row.wechat != null" class="icon-ym icon-ym-weChat">{{ scope.row.wechat }}</i>
                  <!-- <span
                v-else-if="scope.row.contactTypeText=='微信'"><i></i></span>-->
                  <!-- {{ scope.row.contactInfo }} -->
                </template>
              </el-table-column>
              <!-- <el-table-column prop="mobile" label="联系电话" align="center" v-if="custom.hasP('mobile')" width="100" />
              <el-table-column prop="wechat" label="微信" align="left" v-if="custom.hasP('wechat')" width="100" show-overflow-tooltip /> -->
              <el-table-column prop="province" label="省份" align="left" v-if="custom.hasP('province')" width="100" show-overflow-tooltip />
              <el-table-column prop="city" label="城市" align="left" v-if="custom.hasP('province')" width="100" show-overflow-tooltip />
              <el-table-column prop="channel" label="咨询渠道" align="left" v-if="custom.hasP('channel')" width="120" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.channel | dynamicTreeCodeText(channelOptions) }}
                </template>
              </el-table-column>
              <el-table-column prop="lastConsultTime" label="咨询时间" align="center" v-if="custom.hasP('lastConsultTime')" :formatter="custom.tableDateTimeFormat" width="120" />
              <el-table-column prop="lastTrack" label="最近跟进" align="left" width="300" v-if="custom.hasP('lastTrack')" show-overflow-tooltip />
              <el-table-column prop="focusAttention" label="关注的重点" align="left" v-if="custom.hasP('category')" width="120" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.focusAttention | dynamicCodeText(focusAttentionOptions) }}
                </template>
              </el-table-column>
              <el-table-column prop="followupSummary" label="跟进情况概述" align="left" min-width="200" v-if="custom.hasP('lastTrack')" show-overflow-tooltip />
              <el-table-column prop="category" label="客户类别" align=" left" v-if="custom.hasP('category')" width="100" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{ scope.row.category | dynamicCodeText(categoryOptions) }}
                </template>
              </el-table-column>
              <el-table-column prop="createTime" label="录入时间" align="center" v-if="custom.hasP('createTime')" :formatter="custom.tableDateTimeFormat" width="120" />
              <el-table-column prop="createUserName" label="录入人员" align="left" v-if="custom.hasP('createUserName')" width="80" show-overflow-tooltip />
              <el-table-column prop="updateTime" label="修改时间" align="left" v-if="custom.hasP('updateTime')" :formatter="custom.tableDateTimeFormat" width="120" />
              <el-table-column prop="updateUserName" label="修改人员" align="left" v-if="custom.hasP('updateUserName')" width="80" show-overflow-tooltip />
              <el-table-column label="操作" fixed="right" width="80" align="center">
                <template slot-scope="scope">
                  <el-button type="text" size="mini" @click="addSubFormHandle(scope.row.id)">跟进</el-button>
                </template>
              </el-table-column>
            </custom-table>
          </el-tab-pane>
          <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="initData" />
        </el-tabs>
      </div>
    </div>
    <custom-form v-if="formVisible" ref="CustomForm" @refresh="search" />
    <assign-form v-if="assignFormVisible" ref="AssignForm" @refresh="initData" />
    <track-form v-if="trackFormVisible" ref="TrackForm" @refresh="initData" />
    <transfer-form v-if="transferFormVisible" ref="TransferForm" @refresh="initData" />
    <back-form v-if="backFormVisible" ref="BackForm" @refresh="initData" />
    <flow-box v-if="flowVisible" ref="FlowBox" @close="colseFlow" />
  </div>
</template>
<script>
import CustomForm from './Form'
import AssignForm from '../components/AssignForm'
import TrackForm from '../components/TrackForm'
import TransferForm from '../components/TransferForm'
import BackForm from '../components/BackForm'
import OrderForm from '../components/OrderForm'
import FlowBox from '@/views/workFlow/components/FlowBox'

import { getCustomerList, delCustomer, upCustomer, depositCustomer, rollbackCustomer } from '@/api/crm/customer'
export default {
  components: {
    CustomForm,
    AssignForm,
    TrackForm,
    TransferForm,
    BackForm,
    FlowBox
  },
  data() {
    return {
      showAll: false,
      isdisabled: false,
      customer: undefined,
      query: {
        name: undefined,
        area: undefined,
        info: undefined,
        consultTime: undefined,
        channel: undefined,
        managerId: undefined,
        category: undefined,
        status: '1'
      },
      defaultProps: {
        value: 'id',
        children: 'children',
        label: 'fullName',
        disabled: 'selectable'
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'receiveTime'
      },
      formVisible: false,
      trackFormVisible: false,
      transferFormVisible: false,
      backFormVisible: false,
      assignFormVisible: false,
      flowVisible: false,
      channelOptions: [],
      genderOptions: [],
      userList: [],
      categoryOptions: [],
      focusAttentionOptions: []
    }
  },
  computed: {},
  created() {
    this.initData()
    this.getChannelOptions()
    this.getGenderOptions()
    this.getUserList()
    this.getCategoryOptions()
    this.getFocusAttentionOptions()
  },
  methods: {
    getFocusAttentionOptions() {
      this.$store.dispatch('base/getDictionaryData', { sort: 'FocusAttention' }).then(res => {
        this.focusAttentionOptions = this.isDetail ? res : res.filter(o => o.enabledMark == 1)
      })
    },
    getUserList() {
      this.$store.dispatch('base/getUserList').then(res => {
        this.userList = res.filter(o => o.enabledMark == 1 && o.positionId.indexOf('445842201948194053') > -1)
      })
    },
    getChannelOptions() {
      this.$store.dispatch('base/getDictionaryData', { sort: 'ConsultChannels' }).then(res => {
        this.channelOptions = res
      })
    },
    getCategoryOptions() {
      this.$store.dispatch('base/getDictionaryData', { sort: 'CustomerCategory' }).then(res => {
        this.categoryOptions = res
      })
    },
    getGenderOptions() {
      this.$store.dispatch('base/getDictionaryData', { sort: 'sex' }).then(res => {
        this.genderOptions = res
      })
    },
    initData() {
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      getCustomerList
        .call(this, query)
        .then(res => {
          this.list = res.data.list
          this.total = res.data.pagination.total
          this.listLoading = false
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    handleTabClick() {
      this.initData()
    },
    handleSelectionChange(val) {
      this.multipleSelection = this.reduceSame(val)
    },
    // 数组对象去重
    reduceSame(arr) {
      let obj = {}
      return arr.reduce(function(prev, item) {
        obj[item.id] ? '' : (obj[item.id] = 1 && prev.push(item))
        return prev
      }, [])
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(() => {
          delCustomer(id).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData()
              }
            })
          })
        })
        .catch(() => {})
    },
    assignHandle() {
      if (this.multipleSelection.length > 0) {
        this.assignFormVisible = true
        this.$nextTick(() => {
          this.$refs.AssignForm.init(this.multipleSelection)
        })
      }
    },
    transferHandle() {
      if (this.multipleSelection.length > 0) {
        this.transferFormVisible = true
        this.$nextTick(() => {
          this.$refs.TransferForm.init(this.multipleSelection)
        })
      }
    },
    backHandle() {
      if (this.multipleSelection.length > 0) {
        this.backFormVisible = true
        this.$nextTick(() => {
          this.$refs.BackForm.init(this.multipleSelection)
        })
      }
    },
    addSubFormHandle(id) {
      this.trackFormVisible = true
      this.$nextTick(() => {
        this.$refs.TrackForm.init(id)
      })
    },
    depositHandle(row) {
      this.isdisabled = true
      let param = []
      row.status = 4
      param.push(row)
      depositCustomer(param)
        .then(res => {
          this.$message({
            message: res.msg,
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.isdisabled = false
              this.search()
            }
          })
        })
        .catch(() => {
          this.isdisabled = false
        })
    },
    signHandle(row, flowState) {
      let data = {
        id: '',
        enCode: '#visualDevTransition',
        flowId: '334172229149394181',
        formType: 1,
        opType: flowState ? 0 : '-1',
        status: flowState,
        customer: row
      }
      this.flowVisible = true
      this.$nextTick(() => {
        this.$refs.FlowBox.init(data)
      })
    },
    orderHandle(row, flowState) {
      let data = {
        id: '',
        enCode: '#visualDevcustomerpayment',
        flowId: '625865114699957509',
        formType: 1,
        opType: flowState ? 0 : '-1',
        status: flowState,
        customer: row || '',
        typeValue: 'addOrder'
      }
      this.flowVisible = true
      this.$nextTick(() => {
        this.$refs.FlowBox.init(data)
      })
    },
    colseFlow(isrRefresh) {
      this.flowVisible = false
      if (isrRefresh) this.reset()
    },
    upgradeHandle() {
      if (this.multipleSelection.length == 1) {
        this.formVisible = true
        this.$nextTick(() => {
          this.$refs.CustomForm.init(this.multipleSelection[0].id, 3)
        })
      } else {
        this.$message({
          message: '不能进行多选，请检查选择的数据',
          type: 'warning',
          duration: 1000
        })
      }
      // if (this.multipleSelection.length > 0) {
      //   this.isdisabled = true
      //   let data = this.multipleSelection.map(o => {
      //     o.status = 3
      //     return o
      //   })
      //   upCustomer(data)
      //     .then(res => {
      //       this.$message({
      //         message: res.msg,
      //         type: 'success',
      //         duration: 1500,
      //         onClose: () => {
      //           this.isdisabled = false

      //           this.search()
      //         }
      //       })
      //     })
      //     .catch(() => {
      //       this.isdisabled = false
      //     })
      // }
    },
    followHandle() {
      if (this.multipleSelection.length > 0) {
        this.isdisabled = true
        let data = this.multipleSelection.map(o => {
          o.status = 1
          return o
        })
        rollbackCustomer(data)
          .then(res => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.isdisabled = false
                this.search()
              }
            })
          })
          .catch(() => {
            this.isdisabled = false
          })
      }
    },
    addOrUpdateHandle(id) {
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs.CustomForm.init(id)
      })
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'receiveTime'
      }
      this.initData()
    },
    refresh(isrRefresh) {
      this.formVisible = false
      if (isrRefresh) this.reset()
    },
    reset() {
      for (let key in this.query) {
        if (key != 'status') this.query[key] = undefined
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'receiveTime'
      }
      this.initData()
    }
  }
}
</script>
<style lang="scss" scoped>
.crmcustomer {
  .cu-common-layout-main {
    padding: 0;
    .customerTabs {
      height: 100%;
    }
    .line1 {
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
      word-break: break-all;
    }
    >>> .el-tabs__content {
      height: calc(100% - 40px);
      padding: 0;
      .el-tab-pane {
        height: calc(100% - 110px);
        overflow: hidden;
      }
    }
  }
}
</style>
